import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.utils.Drawable

fun main(args: Array<String>)
{
    LwjglApplication(MainPage(), config {
        width = 480
        height = 320
        useGL20 = true
        backgroundFPS = 30
        foregroundFPS = 120
        initialBackgroundColor = Color.GRAY
        resizable = false
        title = "WidgetDemo"
    })
}

class MainPage : ApplicationAdapter()
{
    private lateinit var generator: FreeTypeFontGenerator
    private lateinit var fontData: FreeTypeFontGenerator.FreeTypeBitmapFontData
    private lateinit var bitmapFont: BitmapFont

    private lateinit var stage: Stage
    private lateinit var labelStyle: Label.LabelStyle
    private lateinit var label: Label
    private lateinit var image: Image
    private lateinit var button: ImageButton
    override fun create()
    {
        generator = FreeTypeFontGenerator(Gdx.files.internal("hksn.ttf"))
        fontData = generator.generateDataUnRepeat(20, "你认我做大哥，我教你梳中分-蕾哥哥")
        bitmapFont = BitmapFont(fontData, fontData.textureRegions, false)
        bitmapFont.color = Color.RED
        // 初始化舞台
        stage = Stage()

        // 初始化Label
        labelStyle = Label.LabelStyle(bitmapFont, bitmapFont.color)
        label = Label("蕾哥哥", labelStyle)
        label.setPosition(50f, 50f)
        label.setFontScale(1f)

        // 初始化Image
        image = image(Gdx.files.internal("gift.png"))
        image.scaleX = image.width / 2
        image.scaleY = image.height / 2
        image.setScale(0.6f)
        image.setPosition(Gdx.graphics.floatWidth / 2 - image.width / 2, Gdx.graphics.floatHeight / 2 - image.height / 2)
        // 初始化按钮
        button = ImageButton(drawable("button_normal.png"), drawable("button_click.png"))
        button.setSize(50f,50f)
        button.setPosition(Gdx.graphics.floatWidth - button.width,0f)
        
        stage.addActor(image)
        stage.addActor(label)
        stage.addActor(button)
        
        Gdx.input.inputProcessor = stage
    }

    override fun render()
    {
        clear()
        stage.act()
        stage.draw()
    }
}